var map =
    {
	mapObj: null,
	markers: [],
	init : function () {
	    map.mapObj = new DMap("map",
				  {
				      // point:new DLatLng("37.3218", "126.801"), // test
				      width: "948px",
				      contextMenu: true,
				      reduced_scale: true,
				      map_type: "TYPE_MAP",
				      level:9
				  });
	    


	    var zoom_ctrl = new DZoomControl();
	    var type_ctrl = new DMapTypeControl();
	    var center = map.mapObj.getCenter();

	    map.mapObj.addControl(zoom_ctrl);
	    map.mapObj.addControl(type_ctrl);
	    //map.mapObj.enableDoubleClickZoom();
	    zoom_ctrl.setValign("bottom");
	    map.spreadMarker();
	},
	spreadMarker : function() {
	    // map.mapObj.clearOverlay();
	    var boundary = map.mapObj.getBound();
	    $.getJSON("lib/get_mark_within_bound.php?all=true",
		      function (data) {
			  $("#near-list").empty();
			  $.each(data, function (i, item) {
			      if ( typeof (map.markers[item.id]) == 'undefined' ||
				  map.markers[item.id] == false) {

				  map.displayMarker(item);
				  map.markers[item.id] = true;
				  
				  var li = $("<li/>");
				  var subject;
				  if (item.state == 1)
				  	subject = item.subject + " <strong>[해결]</strong>";
				  else if (item.state == 2)
					subject = item.subject + " <strong>[일부해결]</strong>";	
				  else
					subject = item.subject;
				  $("<a/>").attr('href', "view_report.php?id=" + item.id).html(subject).appendTo(li);
				  li.appendTo("#total-lists");

			      }
			  });
		      });
	},
	displayMarker : function(item) {
	    var point = new DLatLng(Number(item.lat), Number(item.lng));
	    var marker = map.createMarker(point, item); //map.createInfoWindow(point,item);
	    map.mapObj.addOverlay(marker);
	    // map.mapObj.main.overlay.enabled_window = false;
	    // map.mapObj.main.overlay.enabled_single_window = false;
	},

	createInfoWindow : function(coord, item) {
	    var html = '<div><a href="view_report.php?id=' + item.id + '"><img width="' + item.width +'" height="' + item.height + '" src="'+ item.img + '"/></a></div>';
	    var info = new DInfoWindow(html, {point: coord, width:Number(item.width), height:Number(item.height)});

	    return info;
	},

	createMarker : function(point, item) {
	    var info_window_width;
	    var padding;
	    if (Number(item.width) < 110) {
		info_window_width = Number(110);
		padding = (110 - Number(item.width)) / 2;
	    } else {
		info_window_width = Number(item.width)+4;
		padding = 2;
	    }
	    var html = '<div style="padding:2px ' + padding + 'px;width:' + (Number(item.width)+4) + 'px;height:' + (Number(item.height)+4) + 'px" ><a style="" href="view_report.php?id=' + item.id + '"><img width="' + item.width +'" height="' + item.height + '" src="'+ item.img + '"/></a></div>';
	    var info = new DInfoWindow(html, {removable:false,width:info_window_width, height:Number(item.height)+4});

	    var marker = new DMark(point, {infowindow : info, draggable: false});

	    DEvent.addListener(marker, "mouseover", function() {
		this.infowindow.show();
	    });

	    DEvent.addListener(marker, "mouseout", function() {
	//	this.infowindow.hide();
	    });


	    DEvent.addListener(marker, "click", function() {
		window.location.href = "http://diycity.kr/view_report.php?id=" + item.id;
	    });
	    return marker;
	}
	
    };
